package com.ghh.test;


import java.util.Random;

/**
 * 各种id生成策略
 * <p>Title: IDUtils</p>
 * <p>Description: </p>
 * @date	2015年7月22日下午2:32:10
 * @version 1.0
 */
public class IDUtils {

    /**
     * 图片名生成
     */
    public static String genImageName() {
        //取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();
        //long millis = System.nanoTime();
        //加上三位随机数
        Random random = new Random();
        int end3 = random.nextInt(999);
        //如果不足三位前面补0
        String str = millis + String.format("%03d", end3);

        return str;
    }

    /**
     * 商品id生成
     */
    public static long genItemId() {
        //取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();
        //long millis = System.nanoTime();
        //加上两位随机数
        Random random = new Random();
        int end2 = random.nextInt(99);
        //如果不足两位前面补0
        String str = millis + String.format("%02d", end2);
        long id = new Long(str);
        return id;
    }

    /**
     * 生成一些随机数字组合
     */


    public static String getNum(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        return val;

    }
    /**
     * 生成随机数和字母
     */


    public static String getCharAndNum(int length){
        String val = "";
        Random random = new Random();
        for(int i=0;i<length;i++){
            String charorNum = random.nextInt(2)% 2 ==0?"char":"num";
            if("char".equalsIgnoreCase(charorNum)){
                int choice = random.nextInt(2) % 2 ==0 ? 65 : 97;
                val +=(char) (choice + random.nextInt(26));
            }else if("num".equalsIgnoreCase(charorNum)){
                val +=String.valueOf(random.nextInt(10));
            }
        }
        return val;

    }
    /**
     * 对姓名进行隐藏
     */


    public static String hideName(String name) {
        if (name!=null) {
            String newName = name.substring(0, 1);
            int asteriskLen = name.length() - 1;
            for(int i=0;i<asteriskLen;i++){
                newName += "*";
            }
            return newName;
        }
        return name;
    }
    /**
     * 对手机号码进行隐藏中间的几位
     */


    public static String hidePhone(String phone){
        if(phone!="" && phone.length() == 11){
            return phone.substring(0, 3) + "*****" + phone.substring(8);
        }
        return  phone;

    }
    /**
     * 对身份证号码进行隐藏处理
     */


    public static String hideIdCard(String idCard){
        if(idCard!=null && idCard.length()>7){
            String part1 = idCard.substring(0,3);
            String part2 = idCard.substring(idCard.length()-4);
            StringBuffer part3 = new StringBuffer();
            for(int i=0;i<idCard.length()-7;i++){
                part3.append("*");
            }
            return part1+part3+part2;
        }
        return idCard;
    }

    public static void main(String[] args) {
        for(int i=0;i< 100;i++)
            System.out.println(genItemId());
    }
}
